﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace ToolTipTest
{
    class DragControl
    {

        static Cursor cursorMouseDown = Cursors.SizeAll;
        static Cursor cursorMouseOver = Cursors.Hand;
        static Cursor cursorMouseNormal = Cursors.Arrow;

        private static UserControl _control;
        private static Window _window;
        public static bool isMouseDown = false;
        public static void AttachForDrag(Window window, UserControl control)
        {
            _control = control;
            _window = window;
            window.MouseMove += new System.Windows.Input.MouseEventHandler(window_MouseMove);
            window.MouseLeftButtonDown += new System.Windows.Input.MouseButtonEventHandler(window_MouseLeftButtonDown);

            CustomTooltip toolTip = (_control as CustomTooltip);
            toolTip.partialRect.MouseDown += new MouseButtonEventHandler(partialRect_MouseDown);
            toolTip.partialRect.MouseLeftButtonUp += new MouseButtonEventHandler(partialRect_MouseLeftButtonUp);
            toolTip.partialRect.MouseLeave += new MouseEventHandler(partialRect_MouseLeave);
            toolTip.partialRect.MouseEnter += new MouseEventHandler(partialRect_MouseEnter);
        }
        static void changeMouse(Cursor cursor) {
            Mouse.OverrideCursor = cursor;
            Mouse.UpdateCursor();          
        }

        static void partialRect_MouseEnter(object sender, MouseEventArgs e)
        {
            changeMouse(cursorMouseOver);
        }

        static void partialRect_MouseLeave(object sender, MouseEventArgs e)
        {
            changeMouse(cursorMouseNormal);
        }

        static void partialRect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
            changeMouse(cursorMouseOver);
        }

        static void partialRect_MouseDown(object sender, MouseButtonEventArgs e)
        {
            changeMouse(cursorMouseDown);

            CustomTooltip toolTip = (_control as CustomTooltip);
            int controlX = (int)toolTip.UserControlToolTipX;
            int controlY = (int)toolTip.UserControlToolTipY;
            Point mousePos = e.GetPosition(null);
            firstGap = new Point(controlX - mousePos.X, controlY - mousePos.Y);
            isMouseDown = true; 
        }

        static void window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            CustomTooltip toolTip = (_control as CustomTooltip);
            if (toolTip!=null && !(e.OriginalSource is PartiallyRoundedRectangle))
            {
                if (isMouseDown)
                {
                    isMouseDown = false;
                }
            }
        }
        public static Point firstGap = new Point(0, 0);
        static void window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            CustomTooltip toolTip = (_control as CustomTooltip);
            if (toolTip != null && isMouseDown && e.LeftButton == MouseButtonState.Pressed)
            {
                int controlX = (int)toolTip.UserControlToolTipX;
                int controlY = (int)toolTip.UserControlToolTipY;
                Point mousePos = e.GetPosition(null);
                int mouseX = (int)(Mouse.GetPosition(_window).X);
                int mouseY = (int)(Mouse.GetPosition(_window).Y);
                toolTip.UserControlToolTipX = mousePos.X + firstGap.X;
                toolTip.UserControlToolTipY = mousePos.Y + firstGap.Y;
            }
            else {
                             
            }
        }
    }
}
